import datetime
import json
import os
from enum import Enum
import random
import time
import requests
import hashlib


def get_nonce():
    """
        生成一个指定长度的随机字符串
        """
    randomlength = 16
    random_str = ''
    base_str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    length = len(base_str) - 1
    for i in range(randomlength):
        random_str += base_str[random.randint(0, length)]
    return random_str


def signIn(data, index):
    url = 'https://mvip.midea.cn/my/score/create_daily_score'
    headers = {
        'Cookie': data,
        'content-type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.29(0x18001d38) NetType/WIFI Language/zh_CN'
    }
    try:
        print('开始签到')
        response = requests.get(url=url, headers=headers).json()
        print(f'签到返回 {response}')
        return f'第{index}个账号 签到返回: {response}'
    except Exception as e:
        print(f'签到异常 {e}')
        return f'第{index}个账号 签到异常 {e}'


def webhook(message, webhookToken):
    url = f'http://www.pushplus.plus/send'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "token": webhookToken,
        "title": "美的会员",
        "content": message,
        "template": "json"
    }
    response = requests.post(url=url, data=json.dumps(data), headers=headers).json()


def sleepmoment(num):
    random_seconds = random.randint(1, num)
    print(f'休眠{random_seconds}秒')
    time.sleep(random_seconds)


if __name__ == '__main__':
    print('开始执行...')
    sleepmoment(10)
    cks = os.environ['meidi']
    cks = cks.split('@')
    webhookToken = os.environ['PUSH_PLUS_TOKEN']
    msg = ""
    for index, ck in enumerate(cks):
        msg += signIn(ck, index)

    webhook(msg, webhookToken)
